package pl.un4giv3n.mightvsmagic.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

/**
 * @author andrzejewskia
 *	this is the result of the fight between players
 */
@NamedQueries({
	@NamedQuery(name = "find.user.defenses", query = "select fr from FightReport fr where fr.clanWarReport= false and fr.defender.userId = :id and fr.fightTime > :time"),
	@NamedQuery(name = "find.user.unread.defenses", query = "select fr from FightReport fr where fr.clanWarReport= false and fr.defender.userId = :id and fr.fightTime > :time and defenderSeen=false"),
	@NamedQuery(name = "find.user.attacks", query = "select fr from FightReport fr where fr.clanWarReport= false and fr.attacker.userId = :id and fr.fightTime > :time"),
	@NamedQuery(name = "find.all.user.attacks", query = "select fr from FightReport fr where fr.clanWarReport= false and fr.attacker.userId = :id" ),
	@NamedQuery(name = "find.all.user.defenses", query = "select fr from FightReport fr where fr.clanWarReport= false and fr.defender.userId = :id" ),
	@NamedQuery(name = "find.fights.between.users", query = "select fr from FightReport fr where fr.clanWarReport= false and fr.defender.userId = :defId and fr.attacker.userId = :attId order by id desc" )
})
@Entity
public class FightReport {

	/**
	 * id of the repord
	 */
	private Long id;
	/**
	 * defending user data
	 */
	private UserStub defender;
	private String defenderLogin;
	private String defenderDef;
	private String defenderDmg;
	private double defenderIceDmg;
	private double defenderFireDmg;
	private double defenderEarthDmg;
	private double defenderWindDmg;
	private double defenderIceDef;
	private double defenderFireDef;
	private double defenderEarthDef;
	private double defenderWindDef;
	/**
	 * attacking user data
	 */
	private UserStub attacker;
	private String attackerLogin;
	private String attackerDef;
	private String attackerDmg;
	private double attackerIceDmg;
	private double attackerFireDmg;
	private double attackerEarthDmg;
	private double attackerWindDmg;
	private double attackerIceDef;
	private double attackerFireDef;
	private double attackerEarthDef;
	private double attackerWindDef;
	/**
	 * winner login
	 */
	private String winner;
	/**
	 * rounds of the fight
	 */
	private List<Round> rounds = new ArrayList<Round>();
	/**
	 * reward for the winner
	 */
	@Embedded
	private Reward reward;
	private int looserBonusFightExp;
	private int looserBonusMagicExp;
	/**
	 * time of fight
	 */
	private long fightTime;
	private boolean defenderSeen;
	private Long winnerId;
	private boolean clanWarReport = false;

	@SuppressWarnings("unused")
	private FightReport() {
	}

	public FightReport(UserStub attacker, UserStub defender) {
		this.attacker = attacker;
		this.defender = defender;
		this.fightTime = System.currentTimeMillis();
		this.attackerLogin = attacker.getLogin();
		this.defenderLogin = defender.getLogin();
	}

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	void setId(Long id) {
		this.id = id;
	}

	public Reward getReward() {
		return reward;
	}

	public void setReward(Reward reward) {
		this.reward = reward;
	}
	@OneToOne(cascade=CascadeType.ALL)
	public UserStub getDefender() {
		return defender;
	}

	public void setDefender(UserStub defender) {
		this.defender = defender;
	}
	@OneToOne(cascade=CascadeType.ALL)
	public UserStub getAttacker() {
		return attacker;
	}

	public void setAttacker(UserStub attacker) {
		this.attacker = attacker;
	}

	@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
	public List<Round> getRounds() {
		return rounds;
	}

	public void setRounds(List<Round> rounds) {
		this.rounds = rounds;
	}

	public String getWinner() {
		return winner;
	}

	public void setWinner(String winner) {
		this.winner = winner;
	}

	public long getFightTime() {
		return fightTime;
	}

	public void setFightTime(long fightTime) {
		this.fightTime = fightTime;
	}

	public String getDefenderLogin() {
		return defenderLogin;
	}

	public String getAttackerLogin() {
		return attackerLogin;
	}

	void setDefenderLogin(String defenderLogin) {
		this.defenderLogin = defenderLogin;
	}

	void setAttackerLogin(String attackerLogin) {
		this.attackerLogin = attackerLogin;
	}

	public boolean isDefenderSeen() {
		return defenderSeen;
	}

	public void setDefenderSeen(boolean defenderSeen) {
		this.defenderSeen = defenderSeen;
	}

	
	@Override
	public String toString() {
		return "id="+id+"\nattacker="+attacker+"\ndefender="+defender;
	}

	public String getDefenderDef() {
		return defenderDef;
	}

	public void setDefenderDef(String defenderDef) {
		this.defenderDef = defenderDef;
	}

	public String getDefenderDmg() {
		return defenderDmg;
	}

	public void setDefenderDmg(String defenderDmg) {
		this.defenderDmg = defenderDmg;
	}

	public String getAttackerDef() {
		return attackerDef;
	}

	public void setAttackerDef(String attackerDef) {
		this.attackerDef = attackerDef;
	}

	public String getAttackerDmg() {
		return attackerDmg;
	}

	public void setAttackerDmg(String attackerDmg) {
		this.attackerDmg = attackerDmg;
	}

	public double getDefenderIceDmg() {
		return defenderIceDmg;
	}

	public void setDefenderIceDmg(double defenderIceDmg) {
		this.defenderIceDmg = defenderIceDmg;
	}

	public double getDefenderFireDmg() {
		return defenderFireDmg;
	}

	public void setDefenderFireDmg(double defenderFireDmg) {
		this.defenderFireDmg = defenderFireDmg;
	}

	public double getDefenderEarthDmg() {
		return defenderEarthDmg;
	}

	public void setDefenderEarthDmg(double defenderEarthDmg) {
		this.defenderEarthDmg = defenderEarthDmg;
	}

	public double getDefenderWindDmg() {
		return defenderWindDmg;
	}

	public void setDefenderWindDmg(double defenderWindDmg) {
		this.defenderWindDmg = defenderWindDmg;
	}

	public double getDefenderIceDef() {
		return defenderIceDef;
	}

	public void setDefenderIceDef(double defenderIceDef) {
		this.defenderIceDef = defenderIceDef;
	}

	public double getDefenderFireDef() {
		return defenderFireDef;
	}

	public void setDefenderFireDef(double defenderFireDef) {
		this.defenderFireDef = defenderFireDef;
	}

	public double getDefenderEarthDef() {
		return defenderEarthDef;
	}

	public void setDefenderEarthDef(double defenderEarthDef) {
		this.defenderEarthDef = defenderEarthDef;
	}

	public double getDefenderWindDef() {
		return defenderWindDef;
	}

	public void setDefenderWindDef(double defenderWindDef) {
		this.defenderWindDef = defenderWindDef;
	}

	public double getAttackerIceDmg() {
		return attackerIceDmg;
	}

	public void setAttackerIceDmg(double attackerIceDmg) {
		this.attackerIceDmg = attackerIceDmg;
	}

	public double getAttackerFireDmg() {
		return attackerFireDmg;
	}

	public void setAttackerFireDmg(double attackerFireDmg) {
		this.attackerFireDmg = attackerFireDmg;
	}

	public double getAttackerEarthDmg() {
		return attackerEarthDmg;
	}

	public void setAttackerEarthDmg(double attackerEarthDmg) {
		this.attackerEarthDmg = attackerEarthDmg;
	}

	public double getAttackerWindDmg() {
		return attackerWindDmg;
	}

	public void setAttackerWindDmg(double attackerWindDmg) {
		this.attackerWindDmg = attackerWindDmg;
	}

	public double getAttackerIceDef() {
		return attackerIceDef;
	}

	public void setAttackerIceDef(double attackerIceDef) {
		this.attackerIceDef = attackerIceDef;
	}

	public double getAttackerFireDef() {
		return attackerFireDef;
	}

	public void setAttackerFireDef(double attackerFireDef) {
		this.attackerFireDef = attackerFireDef;
	}

	public double getAttackerEarthDef() {
		return attackerEarthDef;
	}

	public void setAttackerEarthDef(double attackerEarthDef) {
		this.attackerEarthDef = attackerEarthDef;
	}

	public double getAttackerWindDef() {
		return attackerWindDef;
	}

	public void setAttackerWindDef(double attackerWindDef) {
		this.attackerWindDef = attackerWindDef;
	}

	public int getLooserBonusFightExp() {
		return looserBonusFightExp;
	}

	public void setLooserBonusFightExp(int looserBonusFightExp) {
		this.looserBonusFightExp = looserBonusFightExp;
	}

	public int getLooserBonusMagicExp() {
		return looserBonusMagicExp;
	}

	public void setLooserBonusMagicExp(int looserBonusMagicExp) {
		this.looserBonusMagicExp = looserBonusMagicExp;
	}

	public void setWinnerId(Long winnerId) {
		this.winnerId = winnerId;
	}

	public Long getWinnerId() {
		return winnerId;
	}
	@Transient
	public String getLooser(){
		if(attackerLogin.equals(winner)){
			return defenderLogin;
		}
		return attackerLogin;
	}

	public boolean isClanWarReport() {
		return clanWarReport;
	}

	public void setClanWarReport(boolean clanWarReport) {
		this.clanWarReport = clanWarReport;
	}

}
